package com.jy.app.lib.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用的数据适配器
 * Created by jan on 2016/4/5.
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    private int layoutId;
    //是否复用
    protected boolean isMultiplex = true;

    public CommonAdapter(Context context, List<T> datas, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = datas;
        this.layoutId = layoutId;
    }

    public CommonAdapter(Context context, List<T> datas, int layoutId,boolean isMultiplex) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = datas;
        this.layoutId = layoutId;
        this.isMultiplex = isMultiplex;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder = CommonViewHolder.get(mContext, convertView, parent,
                layoutId, position,isMultiplex);
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    public abstract void convert(CommonViewHolder holder, T t);

    //向list中添加数据
    public void refreshAdapter(List<T> arrayList) {
        clear();
//        this.mDatas = arrayList;
        mDatas.addAll(arrayList);
        notifyDataSetChanged();
    }
    //清空list集合
    public void clear() {
        mDatas.clear();
        notifyDataSetChanged();
    }
    //移除指定位置的对象
    public void remove(int index) {
        mDatas.remove(index);
        notifyDataSetChanged();
    }
    //移除对象
    public void remove(Object obj) {
        mDatas.remove(obj);
        notifyDataSetChanged();
    }

}